package com.qingcheng.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.order.Myfavorite;
import com.qingcheng.service.order.CartService;
import com.qingcheng.service.order.MyfavoriteService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/myfavorite")
public class MyfavoriteController {

    @Reference
    private MyfavoriteService myfavoriteServicel;

    @Reference
    private CartService cartService;


    /**
     * 添加单个商品到关注
     * @param id
     * @param num
     * @return
     */
    @GetMapping("/add")

    public Result addMyfavorite(String id,Integer num){
        //String username = "menjin";

        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        myfavoriteServicel.add(id,username,num);
       return new Result();
    }

    /**
     * 查询该用户的所有关注商品
     * @param
     */
    @GetMapping("/find")
    public List<Map<String,Object>> findMyfavorites() {
//        String username = "menjin";
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<Map<String, Object>> list = myfavoriteServicel.findList(username);
        return list;
    }
   /* public Result addMyfavorite(String id, Integer num) {
        //String username = "menjin";
            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            myfavoriteServicel.add(id, username, num);
        return new Result();
    }*/

   /* @GetMapping("/find")
    public List<Map<String, Object>> findMyfavorites() {
        String username = "menjin";

        List<Map<String, Object>> list = myfavoriteServicel.findList(username);
        return list;
    }*/


    /**
     * 批量添加我的关注
     * @return
     */
    @GetMapping("/adds")

    public Result addMyfavorites(){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        String username = "menjin";

   /* public Result addMyfavorites() {
        String username = "menjin";*/

        myfavoriteServicel.adds(username);
        return new Result();
    }


    /**
     * 查询我的关注
     * @return
     */
    @GetMapping("/findMyCollection")
    public List<Map<String, Object>> findMyCollection() {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        System.out.println("username获取到了" + skuId);
        List<Map<String, Object>> cartList = myfavoriteServicel.findMyCollection(username);
        System.out.println("myfavoriteServicel结果是:" + cartList);
        return cartList;
    }

    /**
     * 删除单个选中关注
     */
    @GetMapping("/deleteMyCollectionOne")
    public void deleteMyCollectionOne(String skuId){
        //String username = "menjin";
       String username = SecurityContextHolder.getContext().getAuthentication().getName();
        myfavoriteServicel.deleteMyCollectionOne(username,skuId);
    }

    /**
     * 删除所有选中关注
     * @return
     */
    @GetMapping("/deleteMyCollection")
    public Result deleteMyCollection() {
        //String username = "menjin";
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        myfavoriteServicel.deleteMyCollection(username);
        return new Result();
    }


    /**
     * 全选
     * @param checked
     */
    @GetMapping("/selectAll")
    public void selectAll(boolean checked){
        //修改缓存里面的所有商品选中状态
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        myfavoriteServicel.selectAll(username,checked);
    }



    /**
     * 更改购物车选项中状态
     * @param skuId
     * @return
     */
    @GetMapping("/updateMyCollection")
    public Result updateMyCollection(String skuId) {
        //String username = "menjin";
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        myfavoriteServicel.updateMyCollection(username, skuId);
        return new Result();
    }

    //获取刷新单价后的我的关注列表
   /* @GetMapping("/findMyCollectionList")
    public List<Map<String, Object>> findMyCollectionList(String skuId) {
//        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        return myfavoriteServicel.findMyCollectionList(skuId);
    }*/

    /**功能描述
     * 保存商品到购物车
     * @date 2019/9/21 0021
     * @param
     * @return
     */
    @GetMapping("/saveGoodsToCartList")
    public Result saveGoodsToCartList(String skuId, Integer num){
       // String username = "menjin";
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        myfavoriteServicel.saveGoodsToCart(username,skuId,num);
        return new Result();
    }

}
